<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/axios.min.js"></script>
    <script src="js/vue.min.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="ele-2.15.7/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="ele-2.15.7/index.js"></script>
</head>
<body>

<style>

    .text {
        font-size: 14px;
    }

    .item {
        padding: 18px 0;
    }

    .box-card {
        width: 100%;
        height: 200px;
        margin: auto;
        margin-top:30px ;
    }

    .btn-add {
        float: right;
    }



    .btn-add{
        font-size: 14px;

    }
    .operate-container{
        height: 59px;
    }

    /*.pagerstyle{*/
    /*    margin-left: 1247px;*/
    /*}*/

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }

    .container-filter{
        height: 140px;
    }

    .operate-container{
        height: 60px;
        margin-bottom:20px;
    }


</style>

<div id="app">

    <el-card class="box-card container-filter" shadow="never">
        <div class="search-top">
            <i class="el-icon-search"></i>
            <span> 筛选搜索</span>
            <el-button style="float: right;" class="el-button--primary" @click="query(1)">查询结果</el-button>
            <el-button style="float: right; margin-right: 15px" @click="resetInput">重置</el-button>
        </div>
        <div>
            <el-form :inline="true" class="demo-form-inline"
                     style="display: flex; align-items: center">
                <div>
                    <el-form-item label="输入搜索:"  >
                        <el-input v-model="name" placeholder="类型名称" style="padding-right: 0px;width: 219px"></el-input>
                    </el-form-item>

                </div>
                <div>
                    <el-form-item label="主类名称:"  >
                        <el-select v-model="tid" placeholder="请选择">
                            <el-option
                                    v-for="item in typesOption"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>

                </div>
            </el-form>
        </div>
    </el-card>

    <el-card class="box-card operate-container " shadow="never">
        <i class="el-icon-tickets"></i>
        <span>数据列表</span>
        <el-button class="btn-add" size="mini" @click="create" >
            <span>添加</span>
        </el-button>
    </el-card>

    <el-table
            :header-cell-style="{'text-align':'center'}"
            :cell-style="{'text-align':'center'}"
                stripe
              class="el-table--border  "
              :data="tableData.data">
        <el-table-column prop="typedetailid" label="编号" width=""></el-table-column>
        <el-table-column prop="name" label="类型名称"></el-table-column>
        <el-table-column label="设置">
            <template slot-scope="scope">
                <el-button size="medium">属性列表</el-button>
                <el-button size="medium">参数列表</el-button>
            </template>
        </el-table-column>
        <el-table-column prop="tname" label="主类名称"></el-table-column>
        <el-table-column label="操作">
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        @click="handleEdit(scope.$index, scope.row)">编辑
                </el-button>

                <el-button
                        size="mini"
                        type="danger"
                        @click=" remove(scope.row)">删除
                </el-button>

            </template>
        </el-table-column>
    </el-table>

    <el-footer style="text-align: right">
        <el-pagination :page-size="pageSize" @current-change="query" @size-change="sizeChange"
                       background :page-sizes="[10,15,20]"
                       layout="total,sizes,prev, pager,next,jumper"
                       :total="tableData.total"
                       :current-page.sync="tableData.page">
        </el-pagination>
    </el-footer>

    <el-dialog style="width: 880px;margin-left: 350px"  title="类型编辑" :visible.sync="dialogFormVisible" :close-on-click-modal = "false">
        <div style="display: flex; justify-content: space-around;">
            <el-form>
                <el-form-item label="类型名称:" :label-width="formLabelWidth">
                    <el-input v-model="typeData.name" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>

            <el-form>
                <el-form-item label="主类名称:" :label-width="formLabelWidth">
                    <el-select v-model="typeData.tname" placeholder="请选择">
                        <el-option
                                v-for="item in typesOption"
                                :key="item.value"
                                :label="item.label"
                                :value="item.label">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>

        </div>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="saveType">确 定</el-button>
        </div>

    </el-dialog>

</div>
</body>
<script>
    var v = new Vue({
        el : "#app",
        data : {
            typedetailid : "",
            name : "",
            tname : "",
            tid : "",
            formLabelWidth: "100px",
            //表格数据
            tableData: {page:1},
            pageSize: 10,
            typeData :{name:"",tid : "",tname:"",typedetailid:""},
            dialogFormVisible : false,
            typesOption : [],
        },
        created() {
            this.query(1);
            this.queryType();
        },
        methods : {
            query(page) {
                axios.get("ProductTypeServlet.s",{
                    params: {
                        typedetailid : this.typedetailid,
                        name : this.name,
                        tid : this.tid,
                        currentPage : page, //当前页数
                        pageSize: this.pageSize //每页显示条目个数
                    }
                }).then(res=>{
                    this.tableData = res.data;
                    this.tableData.page = page;
                })
            },
            create(){
                this.typeData={name:"",tid : "",tname:"",typedetailid:""};
                this.dialogFormVisible = true;
            },

            sizeChange(page){
                this.pageSize =page;
                this.query(1);
            },
            handleEdit(index,row){
                this.create();
                for (let i in row) {
                   this.typeData[i] = row[i];
                }
                this.dialogFormVisible = true;
            },
            remove(row){
                    this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                        type: 'warning'
                    }).then(() => {
                        axios.get("RemoveMinitypeServlet.s",{
                            params: {
                                typedetailid : row.typedetailid,
                            }
                        }).then(res=>{
                            this.$message({
                                type: 'success',
                                message: res.data.msg,
                            });
                            this.query(1);
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
            },
            saveType(){
                // 构建post参数
                var params = new URLSearchParams();

                for (let name in this.typeData) {
                    params.append(name, this.typeData[name]);
                }

                // 发送 ajax post 请求
                axios.post("SaveMiniTypeServlet.s", params).then(res => {
                    if (res.data.code == 1) {
                        this.$message(res.data.msg);
                        this.query(1); // 刷新表格
                        this.dialogFormVisible = false; // 关闭弹窗
                    } else {
                        this.$alert(res.data.msg);
                    }
                })
            },
            queryType(){
                axios.get("QueryTypeNameServlet.s",null).then(res=>{
                    this.typesOption = res.data;
                })
            },
            resetInput(){
                this.name = "";
                this.tname = "";
            },
        },

    })
</script>

</html>